package com.cloudfun.campusshare.controller;

import cn.hutool.core.map.MapUtil;
import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.RoleType;
import com.cloudfun.campusshare.common.model.dto.AuditDTO;
import com.cloudfun.campusshare.common.model.dto.BatchOperateDTO;
import com.cloudfun.campusshare.common.model.vo.ApplicationSearchVO;
import com.cloudfun.campusshare.common.model.vo.ApplicationVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.ApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Map;

/**
 * Created by Huqin on 2019/12/30
 */
@RestController
@RequestMapping("/v1/application")
@Api(value = "申请/审核模块", description = "申请/审核模块")
@AuthRequired(role = RoleType.ADMIN)
public class ApplicationController {

    @Autowired
    private ApplicationService applicationService;

    @GetMapping("/list")
    @ApiOperation("列表")
    BaseResult<PageResult<ApplicationVO>> search(Pageable pageable,
                                                 ApplicationSearchVO applicationSearchVO) {
        return BaseResultFactory.produceResult(Code.SUCCESS, applicationService.search(applicationSearchVO, pageable));
    }

    @GetMapping("/{id}")
    @ApiOperation("查看详情")
    BaseResult<ApplicationVO> detail(@ApiParam(value = "id", required = true) @NotEmpty(message = "id不能为空") @PathVariable("id") String id) {
        return BaseResultFactory.produceResult(Code.SUCCESS, applicationService.applicationDetail(id));
    }

    @PutMapping("/audit")
    @ApiOperation("审核")
    @AuthRequired(role = {RoleType.ADMIN, RoleType.FINANCIAL_INSTITUTIONS})
    BaseResult<?> audit(@ApiParam(value = "auditDTO", required = true) @NotEmpty(message = "id不能为空") @RequestBody AuditDTO auditDTO) {
        applicationService.audit(auditDTO);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @DeleteMapping
    @ApiOperation("删除")
    BaseResult<?> delete(@ApiParam(value = "BatchOperateDTO", required = true) @RequestBody BatchOperateDTO<ApplicationSearchVO> batchOperateDTO) {
        if (CollectionUtils.isEmpty(batchOperateDTO.getIds()) && null == batchOperateDTO.getSearchData() && !BooleanUtils.isTrue(batchOperateDTO.getAll())) {
            return BaseResultFactory.produceEmptyResult(Code.PARAMETER_ERROR, "参数错误");
        }
        Map<String, String> res;
        if (!CollectionUtils.isEmpty(batchOperateDTO.getIds())) {
            res = applicationService.updateEnabled(batchOperateDTO.getIds());
        } else if (null != batchOperateDTO.getSearchData()) {
            res = applicationService.updateEnabled(batchOperateDTO.getSearchData());
        } else {
            res = applicationService.updateEnabled();
        }
        if (MapUtil.isNotEmpty(res)) {
            return BaseResultFactory.produceResult(Code.ERROR, res);
        }
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }
}
